package com.bible.fire.util;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;

import com.bible.fire.local.AppContext;

public class WindowUtil {
	
	private static String TAG = WindowUtil.class.getSimpleName();
	
	public static LayoutParams getCenterParams(){		
		LayoutParams params = new LayoutParams();
		params.type = LayoutParams.TYPE_SYSTEM_ALERT;
		//params.flags = LayoutParams.FLAG_NOT_TOUCHABLE;
		params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL;
		params.flags = params.flags | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
		params.flags = params.flags | LayoutParams.FLAG_LAYOUT_NO_LIMITS;
		params.format = PixelFormat.RGBA_8888;
		params.alpha = 1.0f;
		//params.gravity = Gravity.TOP|Gravity.LEFT;
		params.gravity = Gravity.CENTER;
		//params.width = 150;
		//params.height = 50;
		//params.x = 50;
		//params.y = 50;
		params.width = LayoutParams.WRAP_CONTENT;
		params.height = LayoutParams.WRAP_CONTENT;
		return params;
	}
	
	public static void showTip(String msg){
		Context context = AppContext.getContext();
		WindowManager wm = (WindowManager)context.getSystemService( Context.WINDOW_SERVICE );
		LayoutParams params = new LayoutParams();
		params.type = LayoutParams.TYPE_SYSTEM_ALERT;
		params.flags = LayoutParams.FLAG_NOT_TOUCHABLE;
		params.flags = params.flags | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
		params.flags = params.flags | LayoutParams.FLAG_LAYOUT_NO_LIMITS;
		params.format = PixelFormat.RGBA_8888;
		params.alpha = 1.0f;
		//params.gravity = Gravity.TOP|Gravity.LEFT;
		params.gravity = Gravity.CENTER;
		//params.width = 150;
		//params.height = 50;
		//params.x = 50;
		//params.y = 50;
		params.width = LayoutParams.WRAP_CONTENT;
		params.height = LayoutParams.WRAP_CONTENT;
		
		TextView tv = new TextView( context );
		tv.setBackgroundColor( Color.GRAY );
		tv.setText( msg );
		tv.setTextSize( 20 );
		tv.setTextColor( Color.WHITE );
		Log.d(TAG, "add view start" );
		wm.addView( tv, params);
		Log.d(TAG, "add view end" );
		/**
		try {
			Thread.sleep( 3000 );
		} catch (Exception e) {
			Log.e(TAG, "thread sleep error...", e);
		}
		Log.d(TAG, "remove view start");
		wm.removeView(tv );
		Log.d(TAG, "remove view end");
		*/
	}
	
	/**
	 * 弹出提示的对话框
	 * @param context
	 * @param msg
	 * @param title
	 */
	public static void showTipDialog( Context context, String msg, String title ){
		AlertDialog.Builder builder = new Builder( context );
		builder.setMessage( msg );
		builder.setTitle( title );
		builder.setPositiveButton("确定", new OnClickListener(){
		   @Override
		   public void onClick(DialogInterface dialog, int which) {
			    dialog.dismiss();
		   }
		});
        builder.create().show();
	}
	
	public static void showOperateDialog( Context context, String msg, String title, Runnable runable ){
		final Runnable frunable = runable;
		AlertDialog.Builder builder = new Builder( context );
		builder.setMessage( msg );
		builder.setTitle( title );
		builder.setPositiveButton("确定", new OnClickListener(){
		   @Override
		   public void onClick(DialogInterface dialog, int which) {
			    dialog.dismiss();
			    frunable.run();
		   }
		});
		builder.setNegativeButton("取消", new OnClickListener() {
		   @Override
		   public void onClick(DialogInterface dialog, int which) {
		      dialog.dismiss();
		   }
		});
        builder.create().show();	
	}

}
